home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: seebs@solutions.solon.com (Peter Seebach)
- Newsgroups: comp.lang.c
- Subject: Re: more problems with qsort
- Date: 1 Mar 1996 17:36:32 -0600
- Organization: Usenet Fact Police (Undercover)
- Message-ID: <4h81m0$avr@solutions.solon.com>
- References: <177399702S86.JW1675A@american.edu> <4h0j9e$ng5@clarknet.clark.net>
- NNTP-Posting-Host: solutions.solon.com
-
- In article <4h0j9e$ng5@clarknet.clark.net>, yom <yom@clark.net> wrote:
- >Since you're trying to sort a char**, the qsort function call must
- >look like this:
-
- >qsort(array,lines,sizeof(char **),(int (*)(void *,void *)) compare);
-
- This is entirely wrong. If you're trying to sort an array of pointers
- to char (or a char ** acting like an array of char *'s), you must use
- qsort(ary, nelem, sizeof(array[0]), compare);
- ... and array[0] will be a char *, which may be different from a
- char **.
-
- >And your compare function must be defined like this:
-
- >int compare(char **a,char **b) {return strcmp(*a,*b);}
-
- No, it really mustn't. It must be defined like this:
- int compare(const void *a, const void *b) {
- return strcmp(*(char **) a, *(char **) b);
- }
-
- >The use of sizeof operator ensures that this code will be compatible
- >with 64 bit architecture such as AXP/OSF.
-
- Except that you took sizeof() the wrong type, and you cast a function
- pointer incorrectly.
-
- -s
- --
- Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
- C/Unix wizard -- C/Unix questions? Send mail for help. No, really!
- FUCK the communications decency act. Goddamned government. [literally.]
- The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
-